package com.itoffer.service.impl;

import com.itoffer.mapper.ApplicantMapper;
import com.itoffer.pojo.Applicant;
import com.itoffer.service.ApplicantService;
import com.itoffer.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;


import java.util.List;

public class ApplicantServiceImpl implements ApplicantService {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();


    @Override
    public Applicant Login(String applicant_email, String applicant_pwd) {
        //2.获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3.获取对应的itofferMapper
        ApplicantMapper mapper = sqlSession.getMapper(ApplicantMapper.class);

        //4.调用方法
        Applicant applicant = mapper.select(applicant_email, applicant_pwd);
        //释放资源
        sqlSession.close();
        return applicant;
    }

    @Override
    public boolean register(Applicant applicant) {
        SqlSession sqlSession = factory.openSession();
        ApplicantMapper mapper = sqlSession.getMapper(ApplicantMapper.class);

        //调用方法
        Applicant a = mapper.selectByApplicant_email(applicant.getApplicant_email());
        if(a==null){
            //用户不存在，提交事务
            mapper.addApplicant(applicant);
            sqlSession.commit();
        }
        //释放资源
        sqlSession.close();
        return a==null;
    }

    @Override
    public boolean selectByEmail(String applicant_email) {
        SqlSession sqlSession = factory.openSession();
        ApplicantMapper mapper = sqlSession.getMapper(ApplicantMapper.class);
        //调用方法
        Applicant a = mapper.selectByApplicant_email(applicant_email);
        sqlSession.close();
        return a!=null;
    }

    @Override
    public boolean updateApplicantPwd(String newApplicant_pwd,int applicant_id) {
        SqlSession sqlSession = factory.openSession();
        ApplicantMapper mapper = sqlSession.getMapper(ApplicantMapper.class);
        int i = mapper.updateApplicantPassword(newApplicant_pwd,applicant_id);
        sqlSession.commit();
//        System.out.println(i+"行受影响");
        sqlSession.close();
        return i>0;
    }
}